home *** CD-ROM | disk | FTP | other *** search
- /* functions for accessing the scrap
- 93/12/24 aih added function to return handle to scrap
- 93/12/18 aih created */
-
- #include <limits.h>
- #include "MemoryLib.h"
- #include "ScrapLib.h"
-
- /* return size of scrap for specified type */
- long ScrapLength(ResType type)
- {
- long len = 0;
- long offset = 0;
-
- len = GetScrap(NULL, type, &offset);
- if (len < 0) {
- if (len != noTypeErr)
- FailOSErr(len);
- len = 0;
- }
- ensure(len >= 0);
- return(len);
- }
-
- /* return offset into scrap of specified type */
- long ScrapOffset(ResType type)
- {
- long len = 0;
- long offset = 0;
-
- require(ScrapLength(type) > 0);
- len = GetScrap(NULL, type, &offset);
- if (len < 0) FailOSErr(len);
- ensure(offset >= 0);
- return(offset);
- }
-
- /* return actual handle to the scrap data, or NULL if no scrap handle */
- Handle ScrapHandle(void)
- {
- PScrapStuff scrap = InfoScrap();
-
- if (! scrap->scrapHandle) {
- LoadScrap();
- scrap = InfoScrap();
- }
- return(scrap->scrapHandle);
- }
-
- /* return copy of scrap data of specified type, or NULL if not in scrap */
- Handle ScrapGet(ResType type)
- {
- Handle scrap = NULL;
- long offset = 0;
- long len = 0;
-
- len = ScrapLength(type);
- if (len > 0) {
- scrap = HandleBegin(len);
- len = GetScrap(scrap, type, &offset);
- if (len < 0) {
- HandleEnd(scrap);
- FailOSErr(len);
- }
- }
- ensure((! scrap && len == 0) || HandleSize(scrap) == len);
- return(scrap);
- }
-
- /* return preferred scrap type when application can accept different types,
- or 0 if no data of requested type in scrap (see IM-1, p460) */
- ResType ScrapPreferredType(const ResType *types, short ntypes)
- {
- long minoffset = LONG_MAX;
- ResType type = 0;
- short i;
-
- for (i = 0; i < ntypes; i++) {
- if (ScrapLength(types[i]) > 0 && ScrapOffset(types[i]) < minoffset) {
- minoffset = ScrapOffset(types[i]);
- type = types[i];
- }
- }
- return(type);
- }
-